package com.tcc.assessment.facultyrole;

import org.apache.xpath.operations.String;
import org.openqa.selenium.By;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import com.tcc.qa.test.util.BaseTest;
import com.tcc.qa.test.util.ButtonFinder;
import com.tcc.qa.test.util.CheckBoxFinder;
import com.tcc.qa.test.util.ErrorHolder;
import com.tcc.qa.test.util.InputFinder;
import com.tcc.qa.test.util.LinkFinder;
import com.tcc.qa.test.util.SelLogger;
import com.tcc.qa.test.util.SelectFinder;
import com.tcc.qa.test.util.SwitchTo;
import com.tcc.qa.test.util.TableFinder;

public class QuestionLibrary extends BaseTest {

  String QuestionLibraryCategory1 = "Selenium";

  @BeforeMethod
  public void beforeTestMethod() {
    System.out.println("Before Test Method");
  }

  @Test(description = "go to class", priority = 1)
  public void classLink() throws InterruptedException {
    LinkFinder.getLinkByText(getWebDriver(), "Academics").click();
    LinkFinder.getLinkByText(getWebDriver(), "0011-Selenium-08 Fall _ Quinty-UML A").click();
    //Assert.assertEquals(getWebDriver().getTitle().toString(), "0011-Selenium-08 Fall _ Quinty-UML A @ Academics>Classes> 0011-Selenium-08 Fall _ Quinty-UML A>Current Class>Welcome");
    Assert.assertTrue(getWebDriver().getTitle().contains("0011-Selenium-08 Fall _ Quinty-UML A"));
    LinkFinder.getLinkByText(getWebDriver(), "Assessments Editor").click();
  }

  @Test(description = "Go to question library", priority = 2)
  public void toQuestionLibrary() throws InterruptedException {
    LinkFinder.getLinkByText(getWebDriver(), "Question Library").click();
  }

  @Test(description = "Create question properties", priority = 3)
  public void createQuestion() throws InterruptedException {
    ButtonFinder.getLinkButton(getWebDriver(), "New Question").click();

    //Check alert without filling the Title
    ButtonFinder.getInputButtonByValue(getWebDriver(), "Save Properties").click();
    Assert.assertEquals(SwitchTo.switchToAlert(getWebDriver()).getText(), "Please enter title for the question");
    System.out.println(SwitchTo.switchToAlert(getWebDriver()).getText());
    getWebDriver().switchTo().alert().accept();

    InputFinder.getInputById(getWebDriver(), "title").sendKeys("Question");
    SelectFinder.getSelectById(getWebDriver(), "difficulty").selectByVisibleText("Difficult");
    InputFinder.getInputById(getWebDriver(), "description").sendKeys("Test create question");

    /**
     * Delete the code due to ticket 33844
    */
    /*
    //Test Add Other Category
    ButtonFinder.getInputButtonByValue(getWebDriver(), "Add Category").click();
    Assert.assertEquals(SwitchTo.switchToAlert(getWebDriver()).getText(),"You must enter a name.");
    getWebDriver().switchTo().alert().accept();
    
    InputFinder.getInputById(getWebDriver(), "otherCategory").sendKeys(QuestionLibraryCategory1);
    ButtonFinder.getInputButtonByValue(getWebDriver(), "Add Category").click();
    Assert.assertEquals(SelectFinder.getSelectText(getWebDriver(), "selCategories", 0), "Selenium");
    Assert.assertEquals(SelectFinder.getSelectOption(getWebDriver(), "selCategories", "Selenium").getText(),QuestionLibraryCategory1);
    
    ButtonFinder.getInputButtonByValue(getWebDriver(), "Add Category").click();
    Assert.assertEquals(SwitchTo.switchToAlert(getWebDriver()).getText(), "Entry already available.  Please choose another name.");
    getWebDriver().switchTo().alert().accept();
    */

    SelectFinder.getSelectOption(getWebDriver(), "categories", "Computer Applications").click();
    //SelectFinder.getSelectById(getWebDriver(), "categories").selectByVisibleText("Computer Applications");
    ButtonFinder.getButtonByTitle(getWebDriver(), "Add").click();
    Assert.assertTrue(SelectFinder.getSelectOption(getWebDriver(), "selCategories", "Computer Applications").isDisplayed());
    SelectFinder.getSelectById(getWebDriver(), "selCategories").selectByVisibleText("Computer Applications");
    ButtonFinder.getButtonByTitle(getWebDriver(), "Remove").click();
    Assert.assertTrue(SelectFinder.isEmpty(getWebDriver(), "selCategories"), "Selected categories is not empty");
    //SelectFinder.getSelectOption(getWebDriver(), "categories", "Computer Applications").clear();
    SelectFinder.getSelectOption(getWebDriver(), "categories", "Computer Knowledge").click();
    ButtonFinder.getButtonByTitle(getWebDriver(), "Add").click();
    InputFinder.getInputById(getWebDriver(), "tag").sendKeys("selenium,test");;
    CheckBoxFinder.getCheckBoxById(getWebDriver(), "privateQ").click();

    ButtonFinder.getInputButtonByValue(getWebDriver(), "Save Properties").click();

    //Check Page Information after Save Properties
//    if (ButtonFinder.isDisplayed(ButtonFinder.getButtonByValue(getWebDriver(), "Make a Copy"))) {
//      MessageLogger.ok();
//    } else {
//      MessageLogger.error("Make a Copy button is not exist!");
//    }

    Assert.assertTrue(ButtonFinder.isDisplayed(ButtonFinder.getButtonByValue(getWebDriver(), "Make a Copy")), "Make a Copy button is not exist!");
    Assert.assertTrue(ButtonFinder.isDisplayed(ButtonFinder.getButtonByValue(getWebDriver(), "Delete Question")), "Delete Question button is not exist!");
    System.out.println(TableFinder.getTableByClass(getWebDriver()).getText());
    Assert.assertTrue(TableFinder.getTableByClass(getWebDriver()).getText().contains("Question"), "Title does not show correctly!");
    Assert.assertTrue(TableFinder.getTableByClass(getWebDriver()).getText().contains("Test create question"), "Description does not show correctly!");
    Assert.assertTrue(TableFinder.getTableByClass(getWebDriver()).getText().contains("admin selenium"), "Creator does not show correctly!");
    Assert.assertTrue(TableFinder.getTableByClass(getWebDriver()).getText().contains("Last Update"), "Last Update does not show correctly!");
    Assert.assertEquals(CheckBoxFinder.getCheckBoxById(getWebDriver(), "privateQ").isSelected(), false, "Mark Question Private function work correctly");

  }

  private static final SelLogger logger = SelLogger.getLogger(QuestionLibrary.class);

  @Test(description = "Create Short Answer", priority = 4)
  public void createShortAnswer() throws InterruptedException {
  
    LinkFinder.getLinkByText(getWebDriver(), "Question").click();   
    ButtonFinder.getButtonByValue(getWebDriver(), "Add New Question").click();
    SwitchTo.getWindowHandle(getWebDriver());
    Assert.assertTrue(getWebDriver().getTitle().contains("Assessment Question Editor"));
    ButtonFinder.getTableButton(getWebDriver(), "Save Question").click();
    logger.log("log me");
    if (SwitchTo.switchToAlert(getWebDriver()).getText().contains("0 and 10000")) {
      logger.pass("");
    } else {
      ErrorHolder.addError(this.getClass());
//      ErrorHolder.hasError(this.getClass());
      logger.error("error me");
    }
    /*
    if(!"The point should be a decimal between 0 and 10000, allow two decimal places at most.".equals(SwitchTo.switchToAlert(getWebDriver()).getText())){
      MessageLogger.ok();
    }else{
      MessageLogger.error("Wrong Message Alert!");
    }
    */
    SwitchTo.switchToAlert(getWebDriver()).accept();
    InputFinder.getInputByName(getWebDriver(), "points").sendKeys("5.0");
    getWebDriver().findElement(By.id("ccEditorrtfDetailObj")).sendKeys("What 's Unit Testing?");
    ButtonFinder.getTableButton(getWebDriver(), "Save Question").click();
    SwitchTo.getWindowHandle(getWebDriver());
 
  }

  @Test(description = "Create Essay", priority = 5)
  public void createEssay() throws InterruptedException {

    ButtonFinder.getButtonByValue(getWebDriver(), "Add New Question").click();
    SwitchTo.getWindowHandle(getWebDriver());
    SelectFinder.getSelectByName(getWebDriver(), "format").selectByVisibleText("Essay");
    InputFinder.getInputByName(getWebDriver(), "points").sendKeys("5.0");
    getWebDriver().findElement(By.id("ccEditorrtfDetailObj")).sendKeys("What's different between black test and white test?");
    ButtonFinder.getTableButton(getWebDriver(), "Save Question").click();
    SwitchTo.getWindowHandle(getWebDriver());
  }

  @Test(description = "Create Fill in Blank", priority = 6)
  public void createFillinBlank() throws InterruptedException {
    ButtonFinder.getButtonByValue(getWebDriver(), "Add New Question").click();
    SwitchTo.getWindowHandle(getWebDriver());
    SelectFinder.getSelectByName(getWebDriver(), "format").selectByVisibleText("Fill in the blank");
    Thread.sleep(1000);
    getWebDriver().findElement(By.id("ccEditorrtfDetailObj")).sendKeys("3*4=?");
    InputFinder.getInputByName(getWebDriver(), "points").sendKeys("10.0");
    SelectFinder.getSelectByName(getWebDriver(), "placement").selectByVisibleText("RIGHT of question");
    InputFinder.getInputByName(getWebDriver(), "fitbAns").sendKeys("12");
    InputFinder.getInputByName(getWebDriver(), "feedback").sendKeys("This is a Fill in the blank question!");
    InputFinder.getInputByName(getWebDriver(), "wildcards").sendKeys("t?o*");
    SelectFinder.getSelectByName(getWebDriver(), "caseSensitivity").selectByVisibleText("No");
    SelectFinder.getSelectByName(getWebDriver(), "ignoreSpace").selectByVisibleText("Yes");
    InputFinder.getInputById(getWebDriver(), "answerFieldFill").sendKeys("twelve");
    ButtonFinder.getInputButtonByValue(getWebDriver(), "Add Alternate Spellings").click();
    ButtonFinder.getTableButton(getWebDriver(), "Save Question").click();
    SwitchTo.getWindowHandle(getWebDriver());

  }

  @AfterTest
  public void endTest() {
    System.out.println("End the test.");
  }

}
